# 测试步骤：
# 1-删除索引
# python C:\Users\kiss\graph_algorithm\shell\import\es_index_delete.py
# 2-创建索引
# python C:\Users\kiss\graph_algorithm\shell\import\es_index_create.py
# 3-导入属性值数据(fake)
# python C:\Users\kiss\graph_algorithm\unittest\test_fake_data\gen_fake_data.py
# '''
# if __name__ == '__main__':
#     # gen_split_csvs()
#     fake_data_to_es()
# '''
# 4-导入属性名数据
# python C:\Users\kiss\graph_algorithm\shell\import\es_import.py
# '''
# if __name__ == '__main__':
#     col2es()
# '''

import os
import sys
import pathlib
import regex as re
from elasticsearch import Elasticsearch

question_parser_path = (pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent /
                        'shell' /
                        'knowledge_graph')
sys.path.append(str(question_parser_path))
print(question_parser_path)
from main import QA

ens_cn_map = {
    'maincontno': '主合约编号',

    # 'resourceitemno': '资源项编号',
    # 'ritypename'    : '资源项名称',

    'buildname': '楼盘名称',
    'subprojname': '楼盘（子项目）名称',
    'custname': '客户名称',
    'custid': '客户编号',
    'idv_lgl_nm': '个人法定名称',
    'cst_id': '客户编号',
    'crdt_no': '证件号码',

    # 'loanacctno'    : '贷款账号',
    # 'n_cst_id'      : '新一代客户编号',
    # 'contno'        : '合约编号',
    # 'cocontno'      : '合作协议编号',
    # 'groupheadname' : '集团总部名称',
}

ens_table_map = {'buildname': 't0202_kgem_building',
                 'subprojname': 't0202_kgem_buildingproject',
                 'custname': 't0202_kgem_enterprise',
                 'custid': 't0202_kgem_enterprise',
                 'maincontno': 't0202_kgem_partnercontract',
                 'cst_id': 't0202_kgem_person',
                 'crdt_no': 't0202_kgem_person',
                 'idv_lgl_nm':'t0202_kgem_person',
                 }

fake_data_path = (pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent /
                  'unittest' /
                  'test_fake_data' /
                  'fake_data_csv')

es = Elasticsearch()

import pandas as pd

raw = pd.read_excel('../../../doc/知识图谱问答系统开发计划.xlsx', sheet_name="问题案例模板")
raw = raw[raw['是否聚合'] != '是']
raw = raw[raw['是否查询子类节点'] != '是']

# data retrieval
top1_cnt = 0
count = 0
result_list = []
for i, each in raw.iterrows():
    node1_table_en = each['问题大类（节点）'].split('|')[1]
    node2_table_en = each['关系/节点'].split('|')[1]
    node2_attr_en = each["问题子类（属性）"].split('|')[1]
    node2_attr_cn = each["问题子类（属性）"].split('|')[0]
    ques_type = each['问题案例测试异常类型']
    raw_question = each['详细问题']
    to_replace = re.findall('\[.*\]', raw_question)[0]  # 替换为真实的姓名或编号

    key_ = re.findall('|'.join(ens_cn_map.keys()), to_replace)[0]
    node1_value = pd.read_csv(str(fake_data_path) + "\\" + ens_table_map.get(key_) + '.csv', dtype=str)[key_].sample(1,random_state=123).iloc[0]
    node1_value = str(node1_value)

    question = raw_question.replace(to_replace, node1_value)

    temp_dict = {'index': i, 'node1_table': node1_table_en, 'node1_value': node1_value,
                 'node2_table': node2_table_en, 'node2_attr_en': node2_attr_en, 'node2_attr_cn': node2_attr_cn,
                 'question': question,
                 'top1_node1_table': None, 'top1_node1_value': None,
                 'top1_node2_table': None, 'top1_node2_en': None, 'top1_node2_cn': None,
                 'top1_node1_score': None, 'top1_node2_score': None,
                 'predict': False,
                 'node1_predict': False,
                 'node1_predict_classifier':None,
                 "node2_predict_classifier":None,
                 'ques_type': ques_type}

    qa = QA()
    qa.answer(question)
    value_list, attr_list = qa.value_list, qa.attr_list
    temp_dict['node1_predict_classifier'] = qa.node1_name
    temp_dict['node2_predict_classifier'] = qa.node2_name

    # exception
    if not len(value_list) or not len(attr_list):
        result_list.append(temp_dict)

    # print(question)
    # print("value_list:{},attr_list:{}".format(value_list,attr_list))
    else:
        # candidate set, select top1
        temp_dict['top1_node1_table'] = value_list[0]['table']
        temp_dict['top1_node1_value'] = value_list[0]['value']
        temp_dict['top1_node2_table'] = attr_list[0]['table']
        temp_dict['top1_node2_en'] = attr_list[0]['attr']
        temp_dict['top1_node2_cn'] = attr_list[0]['value']
        temp_dict['top1_node1_score'] = value_list[0]['score']
        temp_dict['top1_node2_score'] = attr_list[0]['score']

        if node1_table_en == temp_dict['top1_node1_table'] and node1_value == temp_dict['top1_node1_value'] \
                and node2_table_en == temp_dict['top1_node2_table'] and node2_attr_en == temp_dict['top1_node2_en'] \
                and node2_attr_cn == temp_dict['top1_node2_cn']:
            top1_cnt += 1
            temp_dict['predict'] = True
            temp_dict['node1_predict'] = True
            result_list.append(temp_dict)
        else:
            if node1_table_en == temp_dict['top1_node1_table'] and node1_value == temp_dict['top1_node1_value']:
                temp_dict['node1_predict'] = True
            result_list.append(temp_dict)

    count += 1

# 整体抽取的top1准确率
top1_acc = top1_cnt / count
print(top1_acc)

result_path = (pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent /
               'unittest' /
               'shell' /
               'knowledge_graph')

error_df = pd.DataFrame(result_list).to_excel(result_path / "error_case_new_data.xlsx", index=False)
